package com.ruoyi.ewem.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.ewem.domain.Outinfo;
import com.ruoyi.ewem.domain.Physical;
import com.ruoyi.ewem.domain.Warehouse;
import com.ruoyi.ewem.mapper.EwemPhysicalMapper;
import com.ruoyi.ewem.mapper.OutinfoMapper;
import com.ruoyi.ewem.service.WarehouseService;
import com.ruoyi.ewem.mapper.WarehouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;

/**
 * @author 21037
 * @description 针对表【ewem_warehouse(仓库表)】的数据库操作Service实现
 * @createDate 2024-07-27 08:51:54
 */
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse>
        implements WarehouseService {

    @Autowired
    private OutinfoMapper outinfoMapper;

    @Autowired
    private EwemPhysicalMapper physicalMapper;

    @Override
    public List<Warehouse> selectWarehouseList(Warehouse warehouse) {
        return baseMapper.selectWarehouseList(warehouse);
    }

    @Override
    public int updateWarehouse(Warehouse warehouse) {
        return 0;
    }


    /**
     * 出库
     *
     * @param warehouse
     * @return
     */
    @Override
    @Transactional
    public int stockOut(Warehouse warehouse)throws RuntimeException {

        // 根据id查询仓库信息
        Warehouse warehouseDB = baseMapper.selectById(warehouse.getWarehouseId());

        // 判断出库 库存是否充足
        if(warehouseDB.getInventory()<warehouse.getInventory()){
            throw new RuntimeException("出库数量不能大于库存数量");
        }

        // 减库存操作
        Long inventoryDB = warehouseDB.getInventory();
        Long inventory = warehouse.getInventory();
        warehouseDB.setInventory(inventoryDB - inventory);

        // 添加出库记录
        Outinfo outinfo = new Outinfo();
        outinfo.setCreateBy(SecurityUtils.getUsername());
        outinfo.setCreateTime(new Date());
        outinfo.setWarehouseId(warehouseDB.getWarehouseId());
        outinfo.setBatchNum(warehouseDB.getBatchNum());
        outinfo.setCount(warehouse.getInventory().intValue());
        outinfo.setSource(warehouse.getAddress());

        //  根据仓库id查询对应的地址信息
        String destination =  baseMapper.getDestinationById(warehouse.getDestination());
        outinfo.setDestination(destination);

        //  添加物流信息
        Physical physical = new Physical();
        physical.setStartName(warehouse.getAddress());
        physical.setEndName(destination);
        physical.setPhysicalNum(IdUtil.getSnowflakeNextIdStr().toLowerCase());
        physical.setStatus("1");
        physical.setWarehouseId(warehouse.getWarehouseId());
        physical.setCreateBy(SecurityUtils.getUsername());
        physical.setCreateTime(new Date());
        physical.setStartTime(new Date());
        physical.setBatchNum(warehouseDB.getBatchNum());
        // 判断当前司机是否有未完成的任务
//        Physical physicalDB = physicalMapper.selectById(warehouse.getDrivers());
//        if(!StringUtils.isNull(physicalDB)){
//            throw new RuntimeException("当前司机有未完成的运输任务");
//        }
        physical.setDriverId(warehouse.getDrivers());
        physicalMapper.insert(physical);
        outinfo.setPhysicalNum(physical.getPhysicalNum());
        outinfoMapper.insert(outinfo);


        return baseMapper.updateById(warehouseDB);
    }

    @Override
    public Warehouse getByWarehouseId(Integer warehouseId) {
        return baseMapper.getByWarehouseId(warehouseId);
    }

    @Override
    public List<Warehouse> getList() {
        return baseMapper.getList();
    }

    @Override
    public List<Warehouse> selectCounts() {
        return baseMapper.selectCounts();
    }

    @Override
    public List<Warehouse> selectWarehouseById(Integer warehouseId) {
        return baseMapper.selectWarehouseById(warehouseId);
    }


}




